<?php
// +----------------------------------------------------------------------
// | 51Design门户模块
// +----------------------------------------------------------------------
namespace app\schain\controller;

use app\schain\model\SchainTagModel;
use cmf\controller\AdminBaseController;
use think\Db;

/**
 * Class AdminTagController 标签管理控制器
 * @package app\schain\controller
 */
class AdminTagController extends AdminBaseController {
	/**
	 * 企业标签管理
	 * @adminMenu(
	 *     'name'   => '企业标签',
	 *     'parent' => 'schain/AdminIndex/default',
	 *     'display'=> true,
	 *     'hasView'=> true,
	 *     'order'  => 10000,
	 *     'icon'   => '',
	 *     'remark' => '企业标签',
	 *     'param'  => ''
	 * )
	 */
	public function index() {
		$schainTagModel = new SchainTagModel();
		$tags = $schainTagModel->paginate();

		$this->assign("arrStatus", $schainTagModel::$STATUS);
		$this->assign("tags", $tags);
		$this->assign('page', $tags->render());
		return $this->fetch();
	}

	/**
	 * 添加企业标签
	 * @adminMenu(
	 *     'name'   => '添加企业标签',
	 *     'parent' => 'index',
	 *     'display'=> false,
	 *     'hasView'=> true,
	 *     'order'  => 10000,
	 *     'icon'   => '',
	 *     'remark' => '添加企业标签',
	 *     'param'  => ''
	 * )
	 */
	public function add() {
		$schainTagModel = new SchainTagModel();
		$this->assign("arrStatus", $schainTagModel::$STATUS);
		return $this->fetch();
	}

	/**
	 * 添加企业标签提交
	 * @adminMenu(
	 *     'name'   => '添加企业标签提交',
	 *     'parent' => 'index',
	 *     'display'=> false,
	 *     'hasView'=> false,
	 *     'order'  => 10000,
	 *     'icon'   => '',
	 *     'remark' => '添加企业标签提交',
	 *     'param'  => ''
	 * )
	 */
	public function addPost() {

		$arrData = $this->request->param();

		$schainTagModel = new SchainTagModel();
		$schainTagModel->isUpdate(false)->allowField(true)->save($arrData);

		$this->success("保存成功！");

	}

	/**
	 * 更新企业标签状态
	 * @adminMenu(
	 *     'name'   => '更新标签状态',
	 *     'parent' => 'index',
	 *     'display'=> false,
	 *     'hasView'=> false,
	 *     'order'  => 10000,
	 *     'icon'   => '',
	 *     'remark' => '更新标签状态',
	 *     'param'  => ''
	 * )
	 */
	public function upStatus() {
		$intId = $this->request->param("id");
		$intStatus = $this->request->param("status");
		$intStatus = $intStatus ? 1 : 0;
		if (empty($intId)) {
			$this->error("ID 不存在！");
		}

		$schainTagModel = new SchainTagModel();
		$schainTagModel->isUpdate(true)->save(["status" => $intStatus], ["id" => $intId]);

		$this->success("更新成功！");

	}

	/**
	 * 删除企业标签
	 * @adminMenu(
	 *     'name'   => '删除企业标签',
	 *     'parent' => 'index',
	 *     'display'=> false,
	 *     'hasView'=> false,
	 *     'order'  => 10000,
	 *     'icon'   => '',
	 *     'remark' => '删除企业标签',
	 *     'param'  => ''
	 * )
	 */
	public function delete() {
		$intId = $this->request->param("id", 0, 'intval');

		if (empty($intId)) {
			$this->error("ID 不存在！");
		}
		$schainTagModel = new SchainTagModel();

		$schainTagModel->where(['id' => $intId])->delete();
		Db::name('schain_tag_enterprise')->where('tag_id', $intId)->delete();
		$this->success("删除成功！");
	}
}
